package leetcode.editor.cn;

import java.util.ArrayList;
import java.util.List;

/**
 * 分割回文串
 * @author qulq
 * @date 2024-07-07 16:38:33
 */
public class T131_PalindromePartitioning{
	 public static void main(String[] args) {
	 	 //测试代码
	 	 Solution solution = new T131_PalindromePartitioning().new Solution();
	 }
	 
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {

	List<List<String>> ans = new ArrayList<>();
	List<String> path = new ArrayList<>();

    public List<List<String>> partition(String s) {
		backtracking(s, 0);
		return ans;
    }

	private void backtracking(String s, int start) {
		if (start == s.length()) {
			ans.add(new ArrayList<>(path));
			return;
		}
		// 切割的位置从第一位后开始（0，闭区间）,到s末尾（s.length()开区间）
		for (int i = start; i < s.length(); i++) {
			String subStr = s.substring(start, i + 1);
			if (isPalindrome(subStr)) {
				path.add(subStr);
				backtracking(s, i + 1);
				path.remove(path.size() - 1);
			}
		}
	}

	private boolean isPalindrome(String s) {
		for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
			if (s.charAt(i) != s.charAt(j)) {
				return false;
			}
		}
		return true;
	}

}
//leetcode submit region end(Prohibit modification and deletion)

}